/**
 * 实现这个项目的构建任务
 * - sass文件处理任务
 * - js 文件处理任务
 * - html 文件处理任务
 * - 图片文件处理任务
 * - 公共文件 copy 任务
 * - 开发服务器 browser-sync
 * - clean任务 -构建前清除目录
 * - watch 监视文件变化以及构建优化
 * - build 构建线上环境
 * - serve 构建开发环境
 * 
 */


const { src, dest, parallel, series, watch } = require('gulp')

const del = require('del')
const browserSync = require('browser-sync')
/**
 * 自动加载gulp插件
 * - yarn add gulp-load-plugins --dev
 * - gulp-sass  => plugins.sass
 */
const loadPlugins = require('gulp-load-plugins')

const plugins = loadPlugins()

/**
 * 开发服务器
 * - yarn add browser-sync --dev
 */
const bs = browserSync.create() // 创建开发服务器

const cwd = process.cwd() // 返回命令行所在工作目录

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
/**
 * 删除文件
 * - yarn add del --dev
 */
const clean = () => {
  return del(['dist', 'temp'])
}
/**
 * - yarn add node-sass gulp-sass --save-dev
 */
const style = () => {
  console.log(cwd)
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}
/**
 * js文件处理
 * - yarn add @babel/core --dev
 * - yarn add @babel/preset-env --dev
 */
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true })) // 以流的方式往浏览器中推
}
// 页面文件浏览
const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

/**
 * 图片转化
 * - yarn add gulp-imagemin --dev
 */
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}
/**
 * 额外文件处理
 * - 
 */
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('dist'))
}

// 监视变化以及构建优化
const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/*.html', page)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], async function(){
    bs.reload()
  })

  bs.init({
    notify: false, // 浏览器中提示
    port: 2080, // 端口
    // open: false,
    // files: 'dist/**', // browserSync启动后监听文件修改 刷新浏览器
    server: {
      baseDir: ['temp', 'src', 'public'], // 网站根目录
      routes: {
        '/node_modules': 'node_modules' // 请求配置 bootstrap样式
      }
    }
  })
}
/**
 * 合并文件
 * - yarn add gulp-useref --dev
 */
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    // html js css
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
    })))
    .pipe(dest('dist'))
}

const compile = parallel(style, script, page) // 组合任务

// 上线之前执行的任务
const build =  series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  )
)

const develop = series(compile, serve)

module.exports = {
  clean,
  build,
  develop
}

